import {useEffect, useState} from "react";
import {Select} from "antd";
import {ICellInputProps} from "@/biz-components/claim-items/components-v2/cell-input/ICellInputProps";
import * as CommonDataSource from '../../../../common-data-source';

/**
 *
 * data dictionary selector
 *
 * */

export interface SelectByDictionaryExtraProps {
	dictionaryType: string;
	showSearch?: boolean;
}

export function SelectByDictionary(props: SelectByDictionaryExtraProps & ICellInputProps<string>) {
	const [options, setOptions] = useState<Array<{ name: string; value: string }>>([]);

	useEffect(function () {
			CommonDataSource.dictionary.queryDictionaryOnlyFirstLevelForSelect(props.dictionaryType)
				.then(setOptions);
		},
		[props.dictionaryType],
	);

	return (
		<Select<string>
			showSearch={props.showSearch}
			optionFilterProp={'children'}
			disabled={props.disabled}
			value={props.value}
			onChange={(v) => {
				props.onChange(v);
			}}
			allowClear
		>
			{options.map((item) => (
				<Select.Option key={item.value} value={item.value}>
					{item.name}
				</Select.Option>
			))}
		</Select>
	);
}
SelectByDictionary.defaultValue = ''